package com.itheima.ydd.controller;

import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Config;
import com.itheima.ydd.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("sys_config")
public class ConfigController {
    /**
     * update   POST    已测
     * all      GET     已测
     */
    @Autowired
    private ConfigService configService;

    @GetMapping("all")
    public ResultData all() {
        Map<String, Object> map = configService.all();
        return ResultData.ok("", map);
    }

    /**
     * 增加配置
     *
     * @return
     */
    @PostMapping("add")
    public ResultData add(@RequestBody Config config) {
        log.info("增加配置Config:{}", config);
        configService.save(config);
        return ResultData.ok("", "增加配置成功");
    }

    /***
     * Desc:系统配置修改
     * @param configs
     * @return {@link ResultData}
     * @author Cr Mr
     * @date 2022/3/3 17:08
     */
    @PostMapping("update")
    public ResultData update(@RequestBody List<Config> configs) {
        configs.forEach(item -> configService.updateById(item));
        return ResultData.ok("", "");
    }

}